package servlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DBUtil.DBManager;
import entity.Course;
import entity.Elective;
import service.ISmsserivce;
import service.impl.SmsserivceImpl;

/**
 * Servlet implementation class StudentAddServlet
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/StudentAddServlet" })
public class StudentAddServlet extends HttpServlet {

	private ISmsserivce smsservice = new SmsserivceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
//		int course_id = Integer.parseInt(request.getParameter("course_id"));
		int course_id = (int) request.getSession().getAttribute("course_id");
		String username = (String) request.getSession().getAttribute("username");
		//新加
		//String selected = (String) request.getSession().getAttribute("selected");
		Elective elective = new Elective(username,course_id);//获取选修课程
		//新加
	//	Course course=new Course(selected);
		System.out.println(course_id + username);
		try {
			
			int addCourse = smsservice.addCourse(elective);
			
			if(addCourse > 0 ) {
				response.getWriter().write("<script> alert('选课成功');location.href='Studenttable.jsp'</script>");
		        
		        String driver = "com.mysql.jdbc.Driver";

		        try {
	
		            Class.forName(driver);
		           Connection connection = DBManager.getConnection();
		            if(!connection.isClosed()) {
		                System.out.println("Succeeded connecting to the Database!");
		                }
		            Statement statement = connection.createStatement();
		        //    update course set selected=selected+1 where course_id=1001;
		            String sql = "update course set selected=selected+1 where course_id='"+elective.getCourse_id()+"'";
		            //3.ResultSet类，用来存放获取的结果集！！
		            int rs = statement.executeUpdate(sql);
		            connection.close();
		        } catch(ClassNotFoundException e) {
		            //数据库驱动类异常处理
		            System.out.println("Sorry,can`t find the Driver!");
		            e.printStackTrace();
		            } catch(SQLException e) {
		            //数据库连接失败异常处理
		            e.printStackTrace();
		            }catch (Exception e) {
		            // TODO: handle exception
		            e.printStackTrace();
		        }finally{
		        	DBManager.close();//关闭连接
		            System.out.println("数据库数据成功获取！！");
		        }	
				
			}else {
				response.getWriter().write("<script> alert('该课程已选，操作失败');location.href='Studenttable.jsp'</script>");		
			
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
